//
//  XJSecKillGoodsView.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/7/9.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "XJSecKillGoodsView.h"
#import "XJSeckillGoodsCollectionViewCell.h"//秒杀


static NSString * const SeckillCellID = @"XJSeckillGoodsCollectionViewCell";

@interface XJSecKillGoodsView()<UICollectionViewDelegate, UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

/* collectionView */
@property (nonatomic, strong) UICollectionView *collectionView;

/**定时器*/
@property (nonatomic, strong) NSTimer *timer;


@end

@implementation XJSecKillGoodsView

#pragma mark - 一 lazy
- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        //注册
        //cell
        [_collectionView registerClass:[XJSeckillGoodsCollectionViewCell class] forCellWithReuseIdentifier:SeckillCellID];
        
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.backgroundColor = WHITE_COLOR;

    }
    return _collectionView;
}

#pragma mark 二 init
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
        
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeAction) userInfo:nil repeats:YES];
    }
    return self;
}

#pragma mark - 二 - 2 定时器方法
- (void)timeAction {
    
    //model自减
    for (SeckillGoodsModel *model in self.dataArray) {
        
        [model countDown];
    }
    
    //发通知
    [[NSNotificationCenter defaultCenter] postNotificationName:kSecKillGoodsCollectionViewCellNotification object:nil];
}

#pragma mark - 二 - 3 控制器将要消失的时候 销毁 定时器
- (void)xjskGoodsViewInvalidTime {
    [self.timer invalidate];
    self.timer = nil;
}


- (void)setupView {
    self.collectionView.frame = self.bounds;
    [self addSubview:self.collectionView];
}

#pragma mark 三 layout

#pragma mark 四 代理方法
#pragma mark - <UICollectionViewDataSource>
- (NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return self.dataArray.count;
    }else{
        return 0;
    }
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *xjCell = nil;
    if (indexPath.section == 0){//秒杀
        XJSeckillGoodsCollectionViewCell * xjsgCell = [collectionView dequeueReusableCellWithReuseIdentifier:SeckillCellID forIndexPath:indexPath];
        
//        xjsgCell.sgModel = self.dataArray[indexPath.row];
        
        SeckillGoodsModel *model = self.dataArray[indexPath.row];
        
        [xjsgCell configureSecKillGoodsClvCellWithSecKillModel:model indexPath:indexPath];
        
        xjCell = xjsgCell;
    }
    return xjCell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if (kind == UICollectionElementKindSectionHeader){
        return nil;
    }else if (kind == UICollectionElementKindSectionFooter){
        if (indexPath.section==0) {
            return nil;
        }else{
            return nil;
        }
    }else{
        return nil;
    }
}

#pragma mark - item宽高
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0){
        return CGSizeMake((KWidth - 4 )/2, UI_IS_IPHONE5 ? ADAPTHeight(280) : ADAPTHeight(260));
    }else{
        return CGSizeZero;
    }
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewLayoutAttributes *layoutAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    if (indexPath.section == 4) {
        if (indexPath.row == 0) {
            layoutAttributes.size = CGSizeMake(KWidth, KWidth * 0.38);
        }else if (indexPath.row == 1 || indexPath.row == 2 || indexPath.row == 3 || indexPath.row == 4){
            layoutAttributes.size = CGSizeMake(KWidth * 0.5, KWidth * 0.24);
        }else{
            layoutAttributes.size = CGSizeMake(KWidth * 0.25, KWidth * 0.35);
        }
    }
    return layoutAttributes;
}


#pragma mark - head宽高
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    
    return CGSizeZero;
}

#pragma mark - foot宽高
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    if (section == 0) {
        return CGSizeZero;
    }else{
        return CGSizeZero;
    }
}

#pragma mark - <UICollectionViewDelegateFlowLayout>
#pragma mark - X间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    if (section == 1 || section == 2) {
        return 1;
    }else{
        return 0;
    }
}
#pragma mark - Y间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    if (section == 1 || section == 2) {
        return 1;
    }else{
        return 0;
    }
}

#pragma mark - 五 点击cell
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
       SeckillGoodsModel *model = self.dataArray[indexPath.row];
//        XJLog(@"秒杀model:%@",model);
//        XJLog(@"点击了秒杀的第%zd个商品",indexPath.row);
        //        [XJGoodsDetailViewController initWithController:self goodsType:GoodsDetailTypeSecKill];
        
        if (self.xjsecKillGoodsViewClickItemBlock) {
            self.xjsecKillGoodsViewClickItemBlock(model);
        }
    }
}

#pragma mark - 赋值
- (void)setDataArray:(NSMutableArray *)dataArray {
    _dataArray = dataArray;
    
    [self.collectionView reloadData];
}


- (void)dealloc {
    XJLog(@"秒杀商品view被销毁");
    
    //移除通知
    [self removeNotificationCenter];
}

- (void)removeNotificationCenter {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kSecKillGoodsCollectionViewCellNotification object:nil];
}


@end
